package cn.how2go.net.server.protocol;

import cn.how2go.net.server.util.BinaryUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * 终端注册
 */
public class JT_0100 implements JTMessage {
	private static Logger logger = LoggerFactory.getLogger(JT_0100.class);
	/**
	 * 省域ID
	 */
	private short provinceId;
	/**
	 * 市、县域ID
	 */
	private short cityId;
	/**
	 * 制造商ID
	 */
	private String manufactureId;
	/**
	 * 终端型号
	 */
	private String terminalModelNo;
	/**
	 * 终端ID
	 */
	private String terminalId;
	/**
	 * 车牌颜色
	 */
	private byte plateColor = 0;
	/**
	 * 车牌号码
	 */
	private String licenseNo;

	@Override
	public byte[] write() {
		ByteBuf directBuf = Unpooled.directBuffer(16);
		try {
			directBuf.writeShort(provinceId);
			directBuf.writeShort(cityId);
			directBuf.writeBytes(manufactureId.getBytes("GBK"));
			directBuf.writeBytes(terminalModelNo.getBytes("GBK"));
			directBuf.writeBytes(terminalId.getBytes("GBK"));
			directBuf.writeByte(plateColor);
			directBuf.writeBytes(licenseNo.getBytes("GBK"));
		} catch (UnsupportedEncodingException e) {
			logger.error("<<<<组包失败");
			directBuf.release();
			return null;
		}

		byte[] bytess = new byte[directBuf.readableBytes()];
		directBuf.readBytes(bytess);
		directBuf.release();
		return bytess;
	}

	@Override
	public void read(byte[] bytes) {
		ByteBuf directBuf = Unpooled.directBuffer(16);
		directBuf.writeBytes(bytes);
		try {
			this.provinceId = directBuf.readShort();
			this.cityId = directBuf.readShort();
			byte[] manufactureBytes = new byte[5];
			directBuf.readBytes(manufactureBytes);
			this.manufactureId = new String(manufactureBytes, "GBK");
			byte[] terminalModelBytes = new byte[8];
			directBuf.readBytes(terminalModelBytes);
			this.terminalModelNo = new String(terminalModelBytes, "GBK");
			byte[] terminalBytes = new byte[7];
			directBuf.readBytes(terminalBytes);
			this.terminalId = new String(terminalBytes, "GBK");
			this.plateColor = directBuf.readByte();
			byte[] licenseNoBytes = new byte[bytes.length-25];
			directBuf.readBytes(licenseNoBytes);
			this.licenseNo = new String(licenseNoBytes, "GBK");
		}catch (Exception e){
			logger.error("<<<<解包失败"+ BinaryUtil.ToHexFormatString(bytes));
			directBuf.release();
		}
		directBuf.release();
	}

	@Override
	public short getBodyLength() {
		short len = 25;

		try {
			if (licenseNo!= null) {
				byte[] temp = licenseNo.getBytes("GBK");
				len += temp.length;
			}
		} catch (UnsupportedEncodingException e) {
			logger.error("转换时意外错误");
		}

		return len;
	}

	@Override
	public String toString() {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(String.format(
				"省：%1$s,市：%2$s,制造商：%3$s，型号：%4$s，终端：%5$s，车牌颜色：%6$s，车牌号:%7$s",
				getProvinceId(), getCityId(), getManufactureId(),
				getTerminalModelNo(), getTerminalId(), getPlateColor(),
				getLicenseNo()));
		return sBuilder.toString();
	}

	public short getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(short provinceId) {
		this.provinceId = provinceId;
	}

	public short getCityId() {
		return cityId;
	}

	public void setCityId(short cityId) {
		this.cityId = cityId;
	}

	public String getManufactureId() {
		return manufactureId;
	}

	public void setManufactureId(String manufactureId) {
		this.manufactureId = manufactureId;
	}

	public String getTerminalModelNo() {
		return terminalModelNo;
	}

	public void setTerminalModelNo(String terminalModelNo) {
		this.terminalModelNo = terminalModelNo;
	}

	public String getTerminalId() {
		return terminalId;
	}

	public void setTerminalId(String terminalId) {
		this.terminalId = terminalId;
	}

	public byte getPlateColor() {
		return plateColor;
	}

	public void setPlateColor(byte plateColor) {
		this.plateColor = plateColor;
	}

	public String getLicenseNo() {
		return licenseNo;
	}

	public void setLicenseNo(String licenseNo) {
		this.licenseNo = licenseNo;
	}
}